var express = require('express');
var router = express.Router();

var users = [
  { id: 1, role_name: "高级管理员", username: "admin555", passowrd: "admin", mobile: "15602349811", type: "1", email: "12sd3@qq.com", mg_state: true, create_time: "1486720611" },
  { id: 2, role_name: "高级管理员", username: "admin2", passowrd: "admin", mobile: "15602398411", type: "1", email: "12sd3@qq.com", mg_state: true, create_time: "1486720611" },
]
// 查询用户列表响应的数据
var successGet = {
  "data": {
    pagenum: 1,
    total: 0,
    users: []
  },
  "meta": {
    "msg": "获取管理员列表成功",
    "status": 200
  }
}
// 修改用户状态响应的数据
var successPut = {
  "data": {},
  "meta": {
    "msg": "设置状态成功",
    "status": 200
  }
}
// 添加管理员响应数据
var successAddUser = {
  "data": {},
  "meta": {
    "msg": "用户创建成功",
    "status": 201
  }
}
//编辑管理员信息响应数据
var successEditUser = {
  "data": {},
  "meta": {
    "msg": "获取需要编辑的用户数据成功",
    "status": 200
  }
}

// 查询用户列表
router.get('/users', function (req, resp, next) {
  console.log('req.query', req.query)
  successGet.data.users = users
  successGet.data.total = userList.length // 根据名字匹配后的用户数
  resp.send(successGet)
  resp.send()
  // 根据用户传来的页码页数信息过滤   接着发送给浏览器
  if (req.query.query || req.query.query != '') { // 如果query不为空需要匹配query的值进行过滤
    var userList = users.filter(function (item) {
      return item.username.indexOf(req.query.query) != -1
    })
    var numVal = -1
    var userList2 = userList.filter(function (item) {
      numVal++
      return (numVal > (req.query.pagenum - 1) * req.query.pagesize - 1) && (numVal < req.query.pagenum * req.query.pagesize)
    })
    successGet.data.users = userList2
    successGet.data.total = userList.length // 根据名字匹配后的用户数
    resp.send(successGet)

  } else {
    var numVal = -1
    var userList = users.filter(function (item) {
      numVal++
      return (numVal > (req.query.pagenum - 1) * req.query.pagesize - 1) && (numVal < req.query.pagenum * req.query.pagesize)
    })
    successGet.data.users = userList
    successGet.data.total = users.length // 所有用户总数
    resp.send(successGet)
  }
})

// 修改用户状态
router.put('/users/state', function (req, resp, next) {
  console.log('req.query', req.query)
  // 过滤对应ID对象 获取ID对象
  var num = -1
  var userObj = users.filter(function(item){
    num++
    // 更新状态
    if(item.id === parseInt(req.query.id)){
      users[num].mg_state = eval(req.query.type)
    }
    return item.id === parseInt(req.query.id)
  })
  successPut.data = userObj[0]
  if( userObj[0].id === parseInt(req.query.id) & userObj[0].mg_state === eval(req.query.type) ) {
    successPut.meta.status = 200
    successPut.meta.msg = "设置状态成功"
    resp.send(successPut)
  }else{
    successPut.meta.status = 400
    successPut.meta.msg = "设置状态失败"
    resp.send(successPut)
  }
})

// 添加用户管理员
router.post('/users/add', function (req, resp, next) {
  console.log('req.body', req.body)
  var user =   { id: users.length+1, role_name: "高级管理员", username: req.body.username, passowrd: req.body.passowrd, mobile: req.body.mobile, type: "1", email: req.body.email, mg_state: true, create_time: new Date() }
  users.push(user)
  successAddUser.data = user
  resp.send(successAddUser)
})

// 获取编辑用户管理员
router.get('/users/id', function (req, resp, next) {
  console.log('req.query', req.query)
  var useridObj = users.filter(function(item){
    return item.id === parseInt(req.query.id)
  })
  // 补充要响应的数据
  successEditUser.data = useridObj[0]
  resp.send(successEditUser)
})

// 获取编辑用户管理员
router.put('/users/edit', function (req, resp, next) {
  console.log('req.body', req.body)
  var num = -1
  var useridObj = users.filter(function(item){
    num++
    // 将被修改的对应对象覆盖
    if( item.id === parseInt(req.body.id) ){
      console.log('item.id', item.id)
      users[num].email = req.body.email
      users[num].mobile = req.body.mobile
    }
    return item.id === parseInt(req.body.id)
  })
  // 补充要响应的数据
  successEditUser.data = useridObj[0]
  console.log(successEditUser)
  resp.send(successEditUser)
})

// 删除用户管理员
router.delete('/users/delete', function (req, resp, next) {
  console.log('req.query', req.query)
  // 补充要响应的数据
  var newUsers = users.filter(function(item){
    return item.id !== parseInt(req.query.id)
  })
  users = newUsers
  var successDetele = {
    "data": null,
    "meta": {
      "msg": "获取需要编辑的用户数据成功",
      "status": 200
    }
  }
  resp.send(successDetele)
})
// 设置角色操作权限操作
router.put('/users/role/', function (req, resp, next) {
  console.log(req.query)
  var successDetele = {
    "data": null,
    "meta": {
      "msg": "获取需要编辑的用户数据成功",
      "status": 200
    }
  }
  resp.send(successDetele)
})

module.exports = router;
